package com.qf.android.sport.adapter.headline;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.util.Pair;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by wen10 on 2016/12/16.
 */

public class HeadlinePagerAdapter<D> extends PagerAdapter {
    private List<Pair<Integer, Integer>> viewList;
    //private Map<Integer,D> dataMap;
    private ViewDataBinding binding1, binding2;

    public HeadlinePagerAdapter(List<Pair<Integer, Integer>> viewList) {
        this.viewList = viewList;
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View rootView = DataBindingUtil.inflate(LayoutInflater.from(container.getContext()), viewList.get(position).first, container, false).getRoot();


        ViewDataBinding binding = DataBindingUtil.bind(rootView);
        if (position == 0){
            binding1 = binding;
        }else {
            binding2 = binding;
        }
        container.addView(rootView);
        return rootView;
    }

    public void add(int position, D d) {
        switch (position) {
            case 0:
                if (binding1!=null){

                    binding1.setVariable(viewList.get(position).second, d);
                    binding1.executePendingBindings();
                }
                break;
            case 1:
                if (binding2 != null){

                    binding2.setVariable(viewList.get(position).second, d);
                    binding2.executePendingBindings();
                }

//                LayoutHeadlineDynamicrootBinding dynamicrootBinding = (LayoutHeadlineDynamicrootBinding) binding2;
//                dynamicrootBinding.setVariable(viewList.get(position).second,d);
//                dynamicrootBinding.executePendingBindings();
//                Log.d("wen","viewpager ");
//                DraweeController controller = Fresco.newDraweeControllerBuilder()
//                        .setUri("http://lifeixphoto.qiniudn.com/67e/1481700402756_ckqpid.jpg")
//                        .setAutoPlayAnimations(true)
//                        .build();
//                .setController(controller);
                break;
        }
        notifyDataSetChanged();
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}
